package com.sgxy.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgxy.aclservice.entity.AclRole;
import com.sgxy.aclservice.service.AclRoleService;
import com.sgxy.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/aclservice/aclrole")
public class AclRoleController {

    @Autowired
    private AclRoleService aclRoleService;

    //获取角色分页列表
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   @RequestBody(required = false)AclRole role){
        System.out.println(role);
        Page<AclRole> rolePage = new Page<>(page,limit);

        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(role.getRoleName())){
            wrapper.like("role_name", role.getRoleName());
        }
        aclRoleService.page(rolePage,wrapper);
        return R.ok().data("items",rolePage.getRecords()).data("total",rolePage.getTotal());
    }
    @GetMapping("getRoleAll")
    public R getRoleAll(){
        List<AclRole> list = aclRoleService.list(null);
        return R.ok().data("list",list);
    }
    //根据id获取角色
    @GetMapping("getRole/{id}")
    public R getRole(@PathVariable String id){
        AclRole role = aclRoleService.getById(id);
        return R.ok().data("role", role);
    }
    //更新角色信息
    @PostMapping("updateRole")
    public R updateRole(@RequestBody AclRole aclRole){
        aclRoleService.updateById(aclRole);
        return R.ok();
    }
    //添加角色
    @PostMapping("addRole")
    public R addRole(@RequestBody AclRole aclRole){
//        System.out.println(aclRole);
        aclRoleService.save(aclRole);
//        System.out.println(aclRole);
        return R.ok();
    }
    //删除角色
    @DeleteMapping("{id}")
    public R deleteRoleById(@PathVariable String id){
        boolean flag = aclRoleService.deleteRoleById(id);
        return flag?R.ok():R.error();
    }

}

